﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using myOSity.API;

namespace solitaire
{
    public partial class Card : UserControl, IDraggable
    {
        private CardValue _value;
        private CardSuit _Suit;
        private bool _faceUp;
        private const int _faceDownX = 79 * 2;
        private const int _faceDownY = 123 * 4;
        private bool _dragEnabled = true;
        private ICardContainer _previousContainer;

        public Card(CardValue value, CardSuit suit)
        {
            InitializeComponent();
            _value = value;
            _Suit = suit;
            ChangeCard();
            LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
        }
     
        void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RectangleGeometry g = new RectangleGeometry();
            g.Rect = new Rect(0, 0, LayoutRoot.ActualWidth, LayoutRoot.ActualHeight);
            LayoutRoot.Clip = g;
        }

        public CardValue CardValue
        {
            get { return _value; }
        }
        public CardSuit CardSuit
        {
            get { return _Suit; }
        }
        public bool Moveable
        {
            get { return _dragEnabled; }
            set { _dragEnabled = value; }
        }

        public ICardContainer FromContainer
        {
            get { return _previousContainer; }
            set { _previousContainer = value; }
        }

        public Card Clone()
        {
            Card nc = new Card(this.CardValue, this.CardSuit);
            nc.FromContainer = this.FromContainer;
            nc.DragEnabled = this.DragEnabled;
            nc.FaceUp = this.FaceUp;
            return nc;
        }

        public bool FaceUp
        {
            get { return _faceUp; }
            set
            {
                _faceUp = value;
                if (value)
                {
                    imgCard.Margin = new Thickness(-(int)_value, -(int)_Suit, 0, 0);
                }
                else
                {
                    imgCard.Margin = new Thickness(-(int)_faceDownX, -(int)_faceDownY, 0, 0);
                    _dragEnabled = false;
                }
            }
        }

        public CardColor SuitColor
        {
            get
            {
                if (this._Suit == CardSuit.Clubs || this._Suit == CardSuit.Spades)
                    return CardColor.Black;
                else
                    return CardColor.Red;
            }
        }

        private void ChangeCard()
        {
            imgCard.Margin = new Thickness(-(int)_value, -(int)_Suit, 0, 0);
        }

        #region IDraggable Members

        public bool DragEnabled
        {
            get { return _dragEnabled; }
            set { _dragEnabled = value; }
        }

        public FrameworkElement DraggableElement
        {
            get { return this; }
        }

        public void OnLocationChanged()
        {

        }

        public void OnDragEnded()
        {
          //  throw new NotImplementedException();
        }

        public void OnDragStarted()
        {
           // throw new NotImplementedException();
        }

        #endregion
    }

    public enum CardValue
    {
        Ace = 0,
        Two = 79,
        Three = 79 * 2,
        Four = 79 * 3,
        Five = 79 * 4,
        Six = 79 * 5,
        Seven = 79 * 6,
        Eight = 79 * 7,
        Nine = 79 * 8,
        Ten = 79 * 9,
        Jack = 79 * 10,
        Queen = 79 * 11,
        King = 79 * 12
    }

    public enum CardSuit
    {
        Clubs = 0,
        Diamonds = 123,
        Hearts = 123 * 2,
        Spades = 123 * 3
    }

    public enum CardColor
    {
        Red=0,
        Black
    }
}
